<template>
  <!-- // 分配角色 -->
  <div>
    <el-checkbox-group v-model="roleIds">
      <!-- 选中了某一项，值就是label，会装入v-model设置的数组 -->
      <!-- <el-checkbox label="A">apple</el-checkbox> -->
      <el-checkbox v-for="item in list" :key="item.id" :label="item.id"> {{ item.name }}</el-checkbox>
    </el-checkbox-group>
    <div style="margin-top: 20px; text-align: right">
      <el-button type="primary" @click="hSave">确定</el-button>
      <el-button>取消</el-button>
    </div>
  </div>
</template>
<script>
import { getRoles } from '@/api/setting'
import { getUserDetailById } from '@/api/user'
import { assignRoles } from '@/api/employees'
export default {
  props: {
    employeeId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      roleIds: [],
      list: []
    }
  },
  created() {
    this.loadRoles()
    this.loadRolesById()
  },
  methods: {
    async hSave() {
      await assignRoles({ id: this.employeeId, roleIds: this.roleIds })
      this.$emit('success')
    },

    async loadRolesById() {
      const res = await getUserDetailById(this.employeeId)
      console.log('res', res)
      this.roleIds = res.data.roleIds
    },
    // 获取原始数据
    async loadRoles() {
      //  取一个比较大的页码来一次模拟获取所有的数据
      const res = await getRoles({ page: 1, pagesize: 100 })
      this.list = res.data.rows
    }
  }
}
</script>
